package cn.apthink.calendar;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.apthink.utility.Util;
import lombok.Data;

@Data
public class HolidayHelper {
	private DepartmentHelper departmentHelper;
	private List<Holiday> lstHoliday;
	
	public static boolean isHoliday(LocalDate current, List<Holiday> lstHoliday) {
		if (!Util.isEmpty(lstHoliday)) {
			return lstHoliday.stream().filter(x->{
				return x.contain(current);
			}).findAny().isPresent();			
		}
		return false;
	}

	private List<Holiday> getHolidayByDeparment(String id) {
		if (!Util.isEmpty(lstHoliday)) {
			return lstHoliday.stream().filter(x -> {
				return Util.equals(x.getDepartmentId(), id);
			}).collect(Collectors.toList());
		}
		return null;
	}

	/**
	 * 返回生产单位所有假期
	 * 
	 * @param uid
	 * @return
	 * @throws RuntimeException
	 */
	public List<Holiday> getHoliday(String departmentId) throws RuntimeException {
		List<Holiday> result = new ArrayList<Holiday>();
		String id = departmentId;
		List<Holiday> ts;
		while (!Util.isEmpty(id)) {
			ts = getHolidayByDeparment(id);
			if (!Util.isEmpty(ts)) result.addAll(ts);
			id = departmentHelper.getPid(id);
		}
		// David：最后附上缺省假期
		ts = getHolidayByDeparment(null);
		if (!Util.isEmpty(ts)) {
			result.addAll(ts);
		}
		return result;
	}

}
